import request from "../../utils/request";

let startY = 0; //手指起始坐标
let moveY = 0;  //手指移动坐标
let moveDistance = 0; //手指移动的距离

Page({
    data: {
        coverTransform: 'translateY(0)',
        coverTransition: '',
        userInfo: {},
        recentPlayList: []
    },

    onLoad: function (options) {
        const userInfo = wx.getStorageSync('userInfo') || {};
        this.setData({userInfo});
        this.getRecentPlayList(userInfo.userId);
    },

    handleTouchStart(e) {
        startY = e.touches[0].clientY;
        this.setData({coverTransition: ''});
    },

    handleTouchMove(e) {
        moveY = e.touches[0].clientY;
        moveDistance = moveY - startY;
        if (moveDistance <= 0) {
            return;
        }
        if (moveDistance > 80) {
            moveDistance = 80;
        }
        this.setData({coverTransform: `translateY(${moveDistance}rpx)`});
    },

    handleTouchEnd(e) {
        moveY = 0;
        this.setData({
            coverTransform: 'translateY(0rpx)',
            coverTransition: 'all 1s linear'
        });
    },

    toLogin() {
        wx.reLaunch({url: '/pages/login/login'});
    },

    async getRecentPlayList(userId) {
        if (!userId) {
            return;
        }
        const res = await request('/user/record', {uid: userId, type: 0});
        let {allData = []} = res;
        if (allData.length > 10) {
            allData = allData.slice(0, 10);
        }
        allData.map((item, index) => ({...item, id: index + 1}));
        this.setData({recentPlayList: allData});
    }
});
